publicstaticvoidsolve() { inth= io.nextInt(), w = io.nextInt(); boolean[][] g = newboolean[h][w]; for (inti=0; i < h; i++) { Strings= io.next(); for (intj=0; j < w; j++) { if (s.charAt(j) == '#') { g[i][j] = true; } } } intans=0; for (inti=0; i < h; i++) { for (intj=0; j < w; j++) { if (g[i][j]) { ans++; dfs(i, j, g); } } } io.println(ans); }
privatestaticvoiddfs(int x, int y, boolean[][] g) { if (x < 0 || x >= g.length || y < 0 || y >= g[0].length || !g[x][y]) return; g[x][y] = false; for (inti= -1; i <= 1; i++) { for (intj= -1; j <= 1; j++) { dfs(x + i, y + j, g); } } }
publicstaticvoidsolve() { intn= io.nextInt(); long[][] aux = newlong[n][]; for (inti=0; i < n; i++) { longt= io.nextLong(), d = io.nextLong(); aux[i] = newlong[]{t, t + d}; } Arrays.sort(aux, (a, b) -> Long.compare(a[0], b[0]));
inti=0, ans = 0; Queue<Long> q = newPriorityQueue<>(); for (longt=0; ; t++) { if (q.isEmpty()) { if (i == n) break; t = aux[i][0]; } while (i < n && aux[i][0] == t) { q.offer(aux[i++][1]); } while (!q.isEmpty() && q.peek() < t) { q.poll(); } if (!q.isEmpty()) { ans++; q.poll(); } } io.println(ans); }